<template>
  <div>
    <div   class="my-goods-item" v-for="item in goodList" :key="item.id" >
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" @click="toggle(item.id)" v-model="item.goods_state"  id="input"
        >
        <label class="custom-control-label" for="input">
          <img :src="item.goods_img" alt="">
        </label>
      </div>
    </div>
    <div  class="right">
      <div class="top">{{item.goods_name}}</div>
      <div class="bottom">
        <span class="price">¥ {{item.goods_price}}</span>
        <span>
            <MyCount :item="item" @del="delFn" @add="addFn"></MyCount>
        </span>
      </div>
    </div>
  </div>
  </div>
</template>

<script>
import axios from "axios";
import MyCount from "@/components/MyCount"
import eventBus from "@/eventBus"
export default {
  data(){
return {
  goodList :[],

}
  },
    methods:{
     delFn(id){
     const obj = this.goodList.find(item=>item.id === id)
     if(obj.goods_count>0){
     obj.goods_count = obj.goods_count-1
  } },
  addFn(id){
    const obj = this.goodList.find(item=>item.id === id)
     obj.goods_count = obj.goods_count+ 1
  },
  trueFn(is){
    console.log(is);
  },
  toggle(id){
this.goodList.forEach(item=>{
  if(item.id === id){item.goods_state = !item.goods_state}
  eventBus.$emit('goodsreally', this.goodList)
})
  }
    },

    // computed:{
    //  all(){
    //  return this.goodList.every(item=>item.goods_state)
    //  }
    // },
  components:{
      MyCount,
    },
  async created(){
  const {data:{list}} =  await axios({
    method:'GET',
    url:'https://www.escook.cn/api/cart',
})
console.log(list);
this.goodList = list
}
}
</script>

<style lang="less" scoped>
.my-goods-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .left {
    img {
      width: 120px;
      height: 120px;
      margin-right: 8px;
      border-radius: 10px;
    }
    .custom-control-label::before,
    .custom-control-label::after {
      top: 50px;
    }
  }
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .top{
        font-size: 14px;
        font-weight: 700;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}

</style>